package com.example.raising_skills.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.PageUtils;
import com.example.common.dto.BaseReturnDto;
import com.example.customer.entity.CustomerDO;
import com.example.raising_skills.entity.RaisingSkillsDO;
import com.example.raising_skills.mapper.RaisingSkillsMapper;
import com.example.raising_skills.service.RaisingSkillsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.raising_skills.service.ThreadService;
import com.example.view_count.service.ViewCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tc
 * @since 2022-10-30
 */
@Service
public class RaisingSkillsServiceImpl extends ServiceImpl<RaisingSkillsMapper, RaisingSkillsDO> implements RaisingSkillsService {

    @Override
    public BaseReturnDto<RaisingSkillsDO> addSkills(String varieties, String question, String answer) {
        RaisingSkillsDO raisingSkillsDO = new RaisingSkillsDO();
        raisingSkillsDO.setVarieties(varieties);
        raisingSkillsDO.setQuestion(question);
        raisingSkillsDO.setAnswer(answer);

        int i = baseMapper.insert(raisingSkillsDO);
        if (i>0){
            return BaseReturnDto.success().msg("添加成功");
        }else {
            return BaseReturnDto.error().msg("添加失败");
        }
    }

    @Autowired
    private ThreadService threadService;


    @Override
    public PageUtils querylist(Integer pageNum,Integer pageSize,String search) {
        List<RaisingSkillsDO> list = baseMapper.selectList(null);
        Page<RaisingSkillsDO> page = baseMapper.selectPage(new Page<>(pageNum,pageSize), Wrappers.<RaisingSkillsDO>lambdaQuery().like(RaisingSkillsDO::getQuestion,search));
        int total =list.size();
        page.setTotal(total);

        //异步更新日访问数
        threadService.updateViewCount();

        return new PageUtils(page);
    }
}
